package br.com.puertorico.model.template;

import java.util.Properties;

import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ExternalVelocityTemplateDocumentoBuilder extends
		AbstractTemplateDocumentoBuilder {

	private static final Logger LOG = LoggerFactory
			.getLogger(InternalVelocityTemplateDocumentoBuilder.class);

	/**
	 * Prepara o processador de um template, localizado na m&aacute;quina do
	 * usu&aacute;rio
	 *
	 * @param arquivoTemplate
	 *            Nome do template desejado
	 */
	public ExternalVelocityTemplateDocumentoBuilder(String arquivoTemplate) {
		try {
			Properties props = new Properties();
			props.load(this
					.getClass()
					.getClassLoader()
					.getResourceAsStream(
							"ExternalTemplateDocumentoBuilder.properties"));
			Velocity.init(props);
			template = Velocity.getTemplate(arquivoTemplate);
		} catch (ResourceNotFoundException e) {
			LOG.error("Nao foi possivel carregar o template desejado.", e);
			throw new RuntimeException(e);
		} catch (ParseErrorException e) {
			LOG.error("Erro ao processar arquivo de template.", e);
			throw new RuntimeException(e);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
